home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr47 / ucrasm27.zip / SOURCE.ZIP / INSERT.ASM < prev    next >
Assembly Source File  |  1992-03-13  |  5KB  |  195 lines

  1.  
  2. ; Need to include "lists.a" in order to get list structure definition.
  3.  
  4.         include    lists.a
  5.         extrn    sl_malloc:far
  6.  
  7.  
  8. wp        equ    <word ptr>        ;I'm a lazy typist
  9.  
  10.  
  11. ; Special case to handle MASM 6.0 vs. all other assemblers:
  12. ; If not MASM 5.1 or MASM 6.0, set the version to 5.00:
  13.  
  14.         ifndef    @version
  15. @version    equ    500
  16.         endif
  17.  
  18.  
  19.  
  20. StdGrp        group    stdlib,stddata
  21. stddata        segment    para public 'sldata'
  22. stddata        ends
  23.  
  24. stdlib        segment    para public 'slcode'
  25.         assume    cs:stdgrp
  26.  
  27. ; sl_Insert -    DX:SI points at a list node.
  28. ;        ES:DI points at a list.
  29. ;        CX contains a node number.
  30. ;        Insert the new node before the specified node (CX) in the
  31. ;        list.
  32. ;
  33. ; Randall Hyde  3/13/92
  34. ;
  35.  
  36.         public    sl_Insert
  37. sl_Insert    proc    far
  38.         push    ax
  39.         push    bx
  40.         push    cx
  41.         push    ds
  42.         push    es
  43.         push    di
  44.  
  45.         if    @version ge 600
  46.  
  47. ; MASM 6.0 version goes here
  48.  
  49. ; First, locate the CXth node in the list:
  50.  
  51.         mov    ds, dx
  52.         xor    dx, dx
  53.         cmp    wp es:[di].List.Head+2, dx    ;Empty list?
  54.         jne    GetTheNode
  55.  
  56. ; If we get down here, the list is empty!
  57.  
  58.         mov    wp es:[di].List.Head, si
  59.         mov    wp es:[di].List.Head+2, ds
  60.         mov    wp es:[di].List.Tail, si
  61.         mov    wp es:[di].List.Tail+2, ds
  62.         mov    wp es:[di].List.CurrentNode, si
  63.         mov    wp es:[di].List.CurrentNode+2, ds
  64.  
  65.         mov    wp ds:[si].Node.Next, 0            ;Set all the links
  66.         mov    wp ds:[si].Node.Next+2, 0    ; in the first node
  67.         mov    wp ds:[si].Node.Prev, 0        ; to NIL.
  68.         mov    wp ds:[si].Node.Prev+2, 0
  69.         pop    di
  70.         pop    es
  71.         jmp    InsertDone
  72.  
  73.  
  74. ; Okay, we have at least one node in the list, get the pointer to this node
  75. ; into ES:DI.
  76.  
  77. GetTheNode:    les    di, es:[di].List.Head        ;Get ptr to first node
  78.         jmp    short IntoLoop
  79.  
  80. ; The following loop repeats until we reach the end of the list or we count
  81. ; off CX nodes in the list.
  82.  
  83. FindNode:    les    di, es:[di].Node.Next
  84. IntoLoop:    cmp    dx, wp es:[di].Node.Next
  85.         loopne    FindNode
  86.  
  87.         mov    ax, wp es:[di].Node.Prev    ;Get previous ptr.
  88.         mov    bx, wp es:[di].Node.Prev+2
  89.  
  90.         mov    wp es:[di].Node.Prev, si    ;Insert the new node
  91.         mov    wp es:[di].Node.Prev+2, ds    ; before the current
  92.  
  93.         mov    wp ds:[si].Node.Next, di    ;Link in back ptr.
  94.         mov    wp ds:[si].Node.Next+2, es    ; to "current" node.
  95.  
  96.         mov    wp ds:[si].Node.Prev, ax    ;Store away ptr to
  97.         mov    wp ds:[si].Node.Prev+2, bx    ; previous node.
  98.  
  99.         mov    di, ax                ;Get ptr to prev
  100.         mov    es, bx                ; node.
  101.         mov    wp es:[di].Node.Next, si    ;Store away link to
  102.         mov    wp es:[di].Node.Next+2, ds    ; new node.
  103.  
  104.         pop    di                ;Retrieve pointer to
  105.         pop    es                ; list variable.
  106.         mov    wp es:[di].List.CurrentNode, si    ;Store ptr to new
  107.         mov    wp es:[di].List.CurrentNode+2, ds ; current node.
  108.  
  109.  
  110.  
  111.  
  112.         else
  113.  
  114. ; This code is for the other assemblers.
  115.  
  116.  
  117. ; First, locate the CXth node in the list:
  118.  
  119.         mov    ds, dx
  120.         xor    dx, dx
  121.         cmp    wp es:[di].Head+2, dx    ;Empty list?
  122.         je    EmptyList
  123.  
  124. ; Okay, we have at least one node in the list, get the pointer to this node
  125. ; into ES:DI.
  126.  
  127.         les    di, es:[di].Head        ;Get ptr to first node
  128.         jmp    short IntoLoop
  129.  
  130. ; The following loop repeats until we reach the end of the list or we count
  131. ; off CX nodes in the list.
  132.  
  133. FindNode:    les    di, es:[di].Next
  134. IntoLoop:    cmp    dx, wp es:[di].Next
  135.         loopne    FindNode
  136.         jmp    short HasAList
  137.  
  138.  
  139. ; If we get down here, the list is empty!
  140.  
  141. EmptyList:    mov    wp es:[di].Head, si
  142.         mov    wp es:[di].Head+2, ds
  143.         mov    wp es:[di].Tail, si
  144.         mov    wp es:[di].Tail+2, ds
  145.         mov    wp es:[di].CurrentNode, si
  146.         mov    wp es:[di].CurrentNode+2, ds
  147.  
  148.         mov    wp ds:[si].Next, 0            ;Set all the links
  149.         mov    wp ds:[si].Next+2, 0    ; in the first node
  150.         mov    wp ds:[si].Prev, 0        ; to NIL.
  151.         mov    wp ds:[si].Prev+2, 0
  152.         pop    di
  153.         pop    es
  154.         jmp    InsertDone
  155.  
  156. ; If the HEAD pointer is non-NIL, insert the new node before the CXth node in
  157. ; the list down here.
  158.  
  159. HasAList:       mov    ax, wp es:[di].Prev    ;Get previous ptr.
  160.         mov    bx, wp es:[di].Prev+2
  161.  
  162.         mov    wp es:[di].Prev, si    ;Insert the new node
  163.         mov    wp es:[di].Prev+2, ds    ; before the current
  164.  
  165.         mov    wp ds:[si].Next, di    ;Link in back ptr.
  166.         mov    wp ds:[si].Next+2, es    ; to "current" node.
  167.  
  168.         mov    wp ds:[si].Prev, ax    ;Store away ptr to
  169.         mov    wp ds:[si].Prev+2, bx    ; previous node.
  170.  
  171.         mov    di, ax                ;Get ptr to prev
  172.         mov    es, bx                ; node.
  173.         mov    wp es:[di].Next, si    ;Store away link to
  174.         mov    wp es:[di].Next+2, ds    ; new node.
  175.  
  176.         pop    di                ;Retrieve pointer to
  177.         pop    es                ; list variable.
  178.         mov    wp es:[di].CurrentNode, si    ;Store ptr to new
  179.         mov    wp es:[di].CurrentNode+2, ds ; current node.
  180.  
  181.  
  182.  
  183.         endif
  184.  
  185. InsertDone:    pop    ds
  186.         pop    cx
  187.         pop    bx
  188.         pop    ax
  189.         ret
  190.  
  191. sl_Insert    endp
  192.  
  193. stdlib        ends
  194.         end
  195.